package com.lmx;

import io.katharsis.resource.registry.ResourceRegistry;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.MultipartConfigElement;

//import org.hibernate.annotations.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.web.servlet.ServletComponentScan;
//import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import com.lmx.socket.alerm.AlermService;
import com.lmx.socket.alerm.AlermWebSocketHandler;
import com.lmx.socket.alerm.DefaultAlermService;
import com.lmx.socket.alerm.LocalWebSocketHandler;

/**
 * Main Application Class - uses Spring Boot. Just run this as a normal Java
 * class to run up a Jetty Server (on http://localhost:8080) 4
 */
@ServletComponentScan
@Configuration
@ComponentScan
@SpringBootApplication
@EnableScheduling
@EnableAutoConfiguration
@EnableWebSocket
@Import(KathExtendFilter.class)
// @EnableAspectJAutoProxy(proxyTargetClass = true)
// @EnableCaching
public class Application extends SpringBootServletInitializer implements WebSocketConfigurer {
	@Autowired
	private ResourceRegistry resourceRegistry;

	@RequestMapping("/resourcesInfo")
	public Map<?, ?> getResources() {
		final Map<String, String> result = new HashMap<>();
		// Add all resources (i.e. Project and Task)
		for (final Class<?> clazz : resourceRegistry.getResources().keySet()) {
			result.put(resourceRegistry.getResourceType(clazz),
					resourceRegistry.getResourceUrl(clazz));
		}
		return result;
	}

	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		registry.addHandler(alermWebSocketHandler(), "/alerm").setAllowedOrigins("*").withSockJS();
		registry.addHandler(localWebSocketHandler(), "/localServer").setAllowedOrigins("*")
				.withSockJS();
	}

	@Bean
	public WebSocketHandler alermWebSocketHandler() {
		return new AlermWebSocketHandler(alermService());
	}

	@Bean
	/*
	 * 本地主机socket连接
	 */
	public WebSocketHandler localWebSocketHandler() {
		return new LocalWebSocketHandler(alermService());
	}

	@Bean
	public AlermService alermService() {
		// return new DefaultAlermService("Did you say \"%s\"?");
		return new DefaultAlermService("%s");
	}

	@Bean
	public AlermService localServerService() {
		// return new DefaultAlermService("Did you say \"%s\"?");
		return new DefaultAlermService("%s");
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}

	// @Bean
	// public FilterRegistrationBean sessionFilterRegistrationBean(SessionFilter
	// filter) {
	// FilterRegistrationBean registrationBean = new
	// FilterRegistrationBean(filter);
	// registrationBean.setEnabled(false);
	// return registrationBean;
	// }

	@Bean
	public FilterRegistrationBean corsFilterRegistrationBean(CorsDefFilter filter) {
		FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
		registrationBean.setEnabled(false);
		return registrationBean;
	}

	@Bean
	public FilterRegistrationBean encodeFilterRegistrationBean(EncodeFilter filter) {
		FilterRegistrationBean registrationBean = new FilterRegistrationBean(filter);
		registrationBean.setEnabled(false);
		return registrationBean;
	}

	/**
	 * 文件上传配置
	 *
	 * @return
	 */
	@Bean
	public MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
		// 单个文件最大
		factory.setMaxFileSize("10240KB"); // KB,MB
		// / 设置总上传数据总大小
		factory.setMaxRequestSize("102400KB");
		return factory.createMultipartConfig();
	}

	public static void main(final String[] args) {
		SpringApplication.run(Application.class, args);
	}

}